#include "alarmsetting.h"
#include "ui_alarmsetting.h"
#include "desktopnote.h"

AlarmSetting::AlarmSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AlarmSetting)
{
    ui->setupUi(this);
    ui->AlarmDatePushButton->setIcon(QIcon(":/Icon/calendar.ico"));
    ui->AlarmDatePushButton->setIconSize(QSize(32,32));

    setAlarmTimeComboBoxIndex();
    this->setWindowTitle("设置提醒");

    DLG = new QDialog(this);
    DLG->setFixedSize(250,200);
    DLG->setWindowTitle("选择日期");
}

AlarmSetting::AlarmSetting(int t_Month,int t_Day, int t_Week, QString t_MettingAddr, QString t_MettingTime, QString t_EventContent, QWidget *parent):
    calendarMonth(t_Month),
    calendarDay(t_Day),
    calendarWeek(t_Week),
    MettingAddr(t_MettingAddr),
    MettingTime(t_MettingTime),
    EventContent(t_EventContent),
    QWidget(parent),
    ui(new Ui::AlarmSetting)
{
    ui->setupUi(this);
    ui->AlarmDatePushButton->setIcon(QIcon(":/Icon/calendar.ico"));
    ui->AlarmDatePushButton->setIconSize(QSize(32,32));

    setAlarmTimeComboBoxIndex();
    this->setWindowTitle("设置提醒");

    DLG = new QDialog(this);
    DLG->setFixedSize(250,200);
    DLG->setWindowTitle("选择日期");

    QString t_Weeks;
    switch (calendarWeek) {
    case 1:
        t_Weeks = "周一";
        break;
    case 2:
        t_Weeks = "周二";
        break;
    case 3:
        t_Weeks = "周三";
        break;
    case 4:
        t_Weeks = "周四";
        break;
    case 5:
        t_Weeks = "周五";
        break;
    case 6:
        t_Weeks = "周六";
        break;
    case 7:
        t_Weeks = "周日";
        break;
    default:
        break;
    }
    QString t_Str = QString::number(calendarMonth) + "月" + QString::number(calendarDay) + "日," + t_Weeks;

//    int t_Hour = MettingTime.mid(0,2).toInt();
//    int t_Minute = MettingTime.mid(2,2).toInt();
//    //index = 当前时间（累计秒）/ 86400
//    int index = (((t_Hour * 60 * 60) + (t_Minute * 60)) * 100 / 86400);

//    ui->AlarmTimeComboBox->setCurrentIndex(index);

    ui->AlarmTimeComboBox->setCurrentText(MettingTime);
    ui->AlarmDateLabel->setText(t_Str);
    ui->AlarmAddrLineEdit->setText(MettingAddr);
    ui->EventPlainTextEdit->setPlainText(EventContent);
}

AlarmSetting::~AlarmSetting()
{
    delete ui;
}
//取消
void AlarmSetting::on_AlarmCancelPushButton_clicked()
{
    this->close();
}
//保存
void AlarmSetting::on_ALarmEnterPushButton_clicked()
{
    MettingTime = ui->AlarmTimeComboBox->currentText();
    MettingAddr = ui->AlarmAddrLineEdit->text();
    EventContent= ui->EventPlainTextEdit->toPlainText();
    //发送信号
    AlarmEnterSignal(calendarMonth, calendarDay, calendarWeek, MettingTime, MettingAddr, EventContent);
    this->close();
}
//计算当前时间匹配的ComboBox选项，并将ComboBox设置为对应的Index
void AlarmSetting::setAlarmTimeComboBoxIndex(void)
{
    QDateTime DateTime = QDateTime::currentDateTime();

    int Hour= DateTime.toString("hh").toInt();//时
    int Minute = DateTime.toString("mm").toInt();//分
    //index = 当前时间（累计秒）/ 86400
    int index = (((Hour * 60 * 60) + (Minute * 60)) * 100 / 86400);

    ui->AlarmTimeComboBox->setCurrentIndex(index);

    QDate date = QDate::currentDate();
    calendarMonth= date.month();
    calendarDay = date.day();
    calendarWeek= date.dayOfWeek();

    QString t_Week;
    switch (calendarWeek) {
    case 1:
        t_Week = "周一";
        break;
    case 2:
        t_Week = "周二";
        break;
    case 3:
        t_Week = "周三";
        break;
    case 4:
        t_Week = "周四";
        break;
    case 5:
        t_Week = "周五";
        break;
    case 6:
        t_Week = "周六";
        break;
    case 7:
        t_Week = "周日";
        break;
    default:
        break;
    }
    QString t_Str = QString::number(calendarMonth) + "月" + QString::number(calendarDay) + "日," + t_Week;
    ui->AlarmDateLabel->setText(t_Str);
}

//日历
void AlarmSetting::on_AlarmDatePushButton_clicked()
{
    //创建日历
    calendar = new QCalendarWidget(DLG);
    calendar->resize(250, 200);
    calendar->move(0, 0);

//    ui->AlarmTimeComboBox->setCurrentIndex();

    connect(calendar, SIGNAL(clicked(QDate)), this, SLOT(calendarChange(QDate)));
    DLG->show();
    calendar->show();
}

//计算两个日期之间的时间差，用于判断时间先后
int Day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)
{
    int y2, m2, d2;
    int y1, m1, d1;
    /*用于判断日期是否大于3月（2月是判断闰年的标识），还用于纪录到3月的间隔月数*/
    m1 = (month_start + 9) % 12;
    /*如果是1月和2月，则不包括当前年（因为是计算到0年3月1日的天数）*/
    y1 = year_start - m1/10;
    /*365*y1 是不算闰年多出那一天的天数， y1/4 - y1/100 + y1/400  是加所有闰年多出的那一天
    (day_start - 1) 用于计算当前日到1日的间隔天数。*/
    d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);
    /*(m2*306 + 5)/10 用于计算到当前月到3月1日间的天数 306=365-31-28（1月和2月），5是全年中不是31天月份的个数*/
    m2 = (month_end + 9) % 12;
    y2 = year_end - m2/10;
    d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);
    return (d2 - d1);
 }

void AlarmSetting::calendarChange(QDate date)
{
    int Days = 0;
    QDateTime DateTime = QDateTime::currentDateTime();
    Yearlocal = DateTime.toString("yyyy").toInt();//年
    Monthlocal= DateTime.toString("MM").toInt();//月
    Daylocal  = DateTime.toString("dd").toInt();//日

    calendarYear = date.year();
    calendarMonth= date.month();
    calendarDay = date.day();
    calendarWeek= date.dayOfWeek();

    //计算所选时间是否为过去时间
    Days = Day_diff(Yearlocal, Monthlocal, Daylocal, calendarYear, calendarMonth, calendarDay);

    if (Days >= 0)
    {
        QString t_Week;
        switch (calendarWeek) {
        case 1:
            t_Week = "周一";
            break;
        case 2:
            t_Week = "周二";
            break;
        case 3:
            t_Week = "周三";
            break;
        case 4:
            t_Week = "周四";
            break;
        case 5:
            t_Week = "周五";
            break;
        case 6:
            t_Week = "周六";
            break;
        case 7:
            t_Week = "周日";
            break;
        default:
            break;
        }
        QString t_Str = QString::number(calendarMonth) + "月" + QString::number(calendarDay) + "日," + t_Week;
        ui->AlarmDateLabel->setText(t_Str);
    }
    else
    {
        QMessageBox::about(this, "警告", "所选日期不符要求");
    }

    calendar->close();
    DLG->close();
}
